package cn.com.callback

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.elite.EliteBean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.elite.Util;


/**
 * 开发人员： Matt
 * 开发时间：2019年5月28日16:59:15
 * 功能介绍：定时调用此接口，将documentout中符合条件的短信插入水务数据库短信表，由水务进行短信发送；
 *
 */
public class InsertSZWaterDB11 implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(InsertSZWaterDB11.class);
	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response){
		log.debug("-------------------------start  InsertSZWaterDB------------------------");
		//request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("GBK");
		def templistID = request.getParameter("templistID");
		log.debug("templistID="+templistID);
		def templistFlag = request.getParameter("templistFlag");
		log.debug("templistFlag="+templistFlag);

		try{
			def SMSCount = 0;
			def message;
			def msg;
			if(templistFlag == 'cancel'){
				log.debug("作废批次开始执行------------------------------");
				//查询批次下所有数据
				def allDocumentoutSQL = "select count(*) allcount from documentout where sms_guid='${templistID}'";
				log.debug("allDocumentoutSQL="+allDocumentoutSQL);
				List allDocumentoutList = eliteBean.chaxun_sql(allDocumentoutSQL,"dataSource");
				def allcount = allDocumentoutList.get(0).get("allcount");
				//查询批次下已发送的数据
				def sendDocumentoutSQL = "select count(*) sendcount from documentout where sms_guid='${templistID}' and passflag='1' ";
				log.debug("sendDocumentoutSQL="+sendDocumentoutSQL);
				List sendDocumentoutList = eliteBean.chaxun_sql(sendDocumentoutSQL,"dataSource");
				def sendcount = sendDocumentoutList.get(0).get("sendcount");
				if(allcount == sendcount){
					msg= "当前批次共有"+allcount+"条数据，已发送"+sendcount+"条数据，批次无法取消短信发送！";
					log.debug("msg="+msg);
					message = "{\"message\":\""+msg+"\"}";
				}else if(allcount > sendcount){
					def cancelDocumentoutSQL = "update documentout set passflag='2',result='CANCEL' where sms_guid='${templistID}' and passflag is null";
					log.debug("cancelDocumentoutSQL="+cancelDocumentoutSQL);
					def cancelDocumentouFlag = eliteBean.update(cancelDocumentoutSQL,"dataSource");
					if(cancelDocumentouFlag >= 0){
						msg= "当前批次共有"+allcount+"条数据，已发送"+sendcount+"条数据,取消"+cancelDocumentouFlag+"条数据！";
						log.debug("msg="+msg);
						message = "{\"message\":\""+msg+"\"}";
					}
				}else if(sendcount == 0){
					def cancelDocumentoutSQL = "update documentout set passflag='2',result='CANCEL' where sms_guid='${templistID}' and passflag is null";
					log.debug("cancelDocumentoutSQL="+cancelDocumentoutSQL);
					def cancelDocumentouFlag = eliteBean.update(cancelDocumentoutSQL,"dataSource");
					if(cancelDocumentouFlag > 0){
						msg = "当前批次共取消"+cancelDocumentouFlag+"条数据！";
						log.debug("msg="+msg);
						message = "{\"message\":\""+msg+"\"}";
					}
				}
			}else{
				log.debug("发送短信开始执行------------------------------");
				def selectDocumentoutSQL = "select DOCUMENTPATH,EXPECTEDSTARTDATE,EXPECTEDSTARTDATE,documentout_guid,documentto from documentout where expectedStartDate<=GETDATE() and passflag is null and doctype='3'";
				log.debug("selectDocumentoutSQL="+selectDocumentoutSQL);
				List selectDocumentoutList = eliteBean.chaxun_sql(selectDocumentoutSQL,"dataSource");
				if(selectDocumentoutList.size() > 0){
					for(int i = 0;i<selectDocumentoutList.size();i++) {
						Map selectDocumentoutSQLMap = selectDocumentoutList.get(i);
						def DOCUMENTPATH = selectDocumentoutSQLMap.get("DOCUMENTPATH");
						def EXPECTEDSTARTDATE = selectDocumentoutSQLMap.get("EXPECTEDSTARTDATE");
						def documentout_guid = selectDocumentoutSQLMap.get("documentout_guid");
						def documentto = selectDocumentoutSQLMap.get("documentto");
						//插入水务ITSV.szwaterDB.DBO.sendlist表
						def insertSendSQL = "INSERT INTO ITSV.szwaterDB.DBO.sendlist (content, sendtime, schtime, smscount, subcode, bh, status,server, depid, isread, typework, loginname )VALUES('${DOCUMENTPATH}','${EXPECTEDSTARTDATE}','${EXPECTEDSTARTDATE}',1,1,'${documentout_guid}',0,1,1,0,15,'linzm')";
						log.debug("insertSendSQL="+insertSendSQL);
						def insertSendFlag = eliteBean.update(insertSendSQL,"dataSource");
						log.debug("insertSendFlag="+insertSendFlag);
						//插入水务ITSV.szwaterDB.DBO.mobiletemp_1表
						def insertMobiletempSQL = "INSERT INTO ITSV.szwaterDB.DBO.mobiletemp_1( realname, mobile, loginname, bh )VALUES('','${documentto}','','${documentout_guid}')";
						log.debug("insertMobiletempSQL="+insertMobiletempSQL);
						def insertMobiletempFlag = eliteBean.update(insertMobiletempSQL,"dataSource");
						log.debug("insertMobiletempFlag="+insertMobiletempFlag);
						if(insertSendFlag==1&&insertMobiletempFlag==1){
							SMSCount++;
							def updateDocumentoutSQL = "update documentout set passflag='1' where documentout_guid='${documentout_guid}'";
							log.debug("updateDocumentoutSQL="+updateDocumentoutSQL);
							def updateDocumentouFlag = eliteBean.update(updateDocumentoutSQL,"dataSource");
						}
					}
					msg= "当前批次共有"+selectDocumentoutList.size()+"条数据，成功插入"+SMSCount+"条！";
					log.debug("msg="+msg);
					message = "{\"message\":\""+msg+"\"}";
				}else {
					message = '{"message":"当前批次下没有需要发送短信的数据"}';
				}
			}

			return message;
		}catch(Exception e){
			log.error("接口异常信息：" ,e);
		}
	}
}
